package com.kaiwukj.android.ufamily.mvp.ui.widget.home;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.AttributeSet;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.blankj.utilcode.util.LogUtils;

/**
 * 商超webview
 */
public class ServiceWebView extends WebView {

    private Context mContext;

    private onPageStateListener mPageStateListener;

    private String code = "0";

    public void setOnPageStateListener(onPageStateListener onPageStateListener) {
        mPageStateListener = onPageStateListener;
    }

    public ServiceWebView(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public ServiceWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public ServiceWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    @SuppressLint("AddJavascriptInterface")
    private void init() {
        WebSettings settings = this.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setSaveFormData(true);
        settings.setSavePassword(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        settings.setDefaultZoom(WebSettings.ZoomDensity.MEDIUM);
        settings.setPluginState(WebSettings.PluginState.ON);
        settings.setSupportMultipleWindows(true);
        settings.setDomStorageEnabled(true);
        settings.setGeolocationEnabled(true);
        settings.setAppCacheMaxSize(Long.MAX_VALUE);
        settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setJavaScriptCanOpenWindowsAutomatically(true);
        //启用数据库
        settings.setDatabaseEnabled(true);
        //设置定位的数据库路径
        String dir = getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        settings.setGeolocationDatabasePath(dir);
        //启用地理定位
        settings.setGeolocationEnabled(true);
        //开启DomStorage缓存
        settings.setDomStorageEnabled(true);


        //设置本地调用对象及其接口
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }


        setWebChromeClient(new WebChromeClient());
        setWebViewClient(webViewClient);

        // addJavascriptInterface(new ShopInterface(mContext), "shop");
    }

    private WebViewClient webViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            // 防止加载网页时调起系统浏览器
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                view.loadUrl(url);
                return true;
            }
            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (mPageStateListener != null) {
                mPageStateListener.onPageStarted(view, url, favicon);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (mPageStateListener != null) {
                mPageStateListener.onPageFinished(view, url);
            }
            //view.loadUrl("javascript:ce()");
            /*post(() ->
                    onPayOk());*/

            //post(()->onPayResult(11));
        }
    };

    private void showLoading() {
    }

    private void hideLoading() {
    }

    /*public void goBack() {
        String methodStr = "javascript:goBack()";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            evaluateJavascript(methodStr, value -> {
                code = value;
            });
        }
        return code;
    }*/

    //是否由js拦截
    public boolean allowGoBack() {
        String methodStr = "javascript:allowGoBack()";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            evaluateJavascript(methodStr, value ->
                    {
                        code = value;
                        LogUtils.d("value-------------------->" + value);
                    }
            );
        }
        return "1".equals(code);
    }


    public interface onPageStateListener {
        void onPageStarted(WebView view, String url, Bitmap favicon);

        void onPageFinished(WebView view, String url);
    }

}
